package com.shfl;

import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.util.Callback;
import sun.misc.BASE64Decoder;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.ResourceBundle;

public class Controller implements Initializable {

    @FXML
    private TableView testTable;


    @FXML
    private TableColumn tc1;
    @FXML
    private TableColumn tc2;
    @FXML
    private TableColumn tc3;

    @Override
    public void initialize(URL location, ResourceBundle resources) {


        insertTableView();
    }


    public void insertTableView() {
        final ObservableList<Person> data
                = FXCollections.observableArrayList(
                new Person("Jacob", "Smith"),
                new Person("Isabella", "Johnson"),
                new Person("Ethan", "Williams"),
                new Person("Emma", "Jones"),
                new Person("Michael", "Brown")
        );
        tc1.setCellValueFactory(new PropertyValueFactory<>("firstName"));
        tc2.setCellValueFactory(new PropertyValueFactory<>("lastName"));

        //操作按钮
        Callback<TableColumn<Person, String>, TableCell<Person, String>> cellFactory
                = //
                new Callback<TableColumn<Person, String>, TableCell<Person, String>>() {
                    @Override
                    public TableCell call(final TableColumn<Person, String> param) {
                        final TableCell<Person, String> cell = new TableCell<Person, String>() {

                            final Button btn = new Button("Just Do It");

                            @Override
                            public void updateItem(String item, boolean empty) {
//                                if(){
//
//                                }


                                super.updateItem(item, empty);
                                if (empty) {
                                    setGraphic(null);
                                    setText(null);
                                } else {
                                    btn.setOnAction(event -> {
                                        Person person = getTableView().getItems().get(getIndex());
                                        System.out.println(person.getFirstName()
                                                + "   " + person.getLastName());
                                    });
                                    setGraphic(btn);
                                    setText(null);
                                }
                            }
                        };
                        return cell;
                    }
                };
        tc3.setCellFactory(cellFactory);

        testTable.setItems(data);

    }

    public static class Person {

        private final SimpleStringProperty firstName;
        private final SimpleStringProperty lastName;

        private Person(String fName, String lName) {
            this.firstName = new SimpleStringProperty(fName);
            this.lastName = new SimpleStringProperty(lName);
        }

        public String getFirstName() {
            return firstName.get();
        }

        public void setFirstName(String fName) {
            firstName.set(fName);
        }

        public String getLastName() {
            return lastName.get();
        }

        public void setLastName(String fName) {
            lastName.set(fName);
        }

    }


    /**
     * 根据base64的图片获取输入流
     *
     * @param val
     * @return
     */
//    public InputStream getInputStreamByBease64(String val) {
//        val = val.replaceAll("data:image/png;base64,", "");
//        BASE64Decoder decoder = new BASE64Decoder();
//        byte[] imageByte = null;
//        try {
//            imageByte = decoder.decodeBuffer(val);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return new ByteArrayInputStream(imageByte);
//    }


}
